import {
	useSpinStore
} from '@/store/modules/spin';
import {
	message,
	Modal
} from 'ant-design-vue';
import {
	airSentry
} from '@/utils/air-sentry';
import {
	encryptData
} from '@/utils/encrypt';
import {
	h
} from 'vue';
import {
	ExclamationCircleOutlined
} from '@ant-design/icons-vue';
import moment from 'moment';

const typeToTime = function(type){
	let syear = '';
	let now = new Date();
	let eyear = moment(now).format('YYYY-MM-DD');
	//近一年
	if (type == "b") {
		syear = moment(now).subtract(1, 'Y').format("YYYY-MM-DD");
	}
	//近一月
	else if (type == "c") {
		syear = moment(now).subtract(1, 'M').format("YYYY-MM-DD");
	}
	//近1周
	else if (type == "d") {
		syear = moment(now).subtract(7, 'd').format("YYYY-MM-DD");
	}
	//昨日
	else if (type == "e") {
		syear = moment(now).subtract(1, 'd').format("YYYY-MM-DD");
		eyear = syear;
	}
	//今日
	else if (type == "f") {
		syear = eyear;
	}
	return syear + "至" + eyear;
}

export default {
	/**
	 * 显示loading
	 */
	showLoading: () => {
		useSpinStore().show();
	},
	/**
	 * 关闭loading
	 */
	hideLoading: () => {
		useSpinStore().hide();
	},
	/**
	 * 错误消息
	 */
	error: (msg) => {
		console.log(msg);
		message.error(msg);
	},
	warning: (msg) => {
		console.log(msg);
		message.warning(msg);
	},
	/**
	 * 成功消息
	 */
	success: (msg) => {
		console.log(msg);
		message.success(msg);
	},
	/**
	 * 销毁消息
	 */
	destroy: () => {
		message.destroy();
	},
	showConfirm: (option) => {
		Modal.confirm({
			title: option.title ? option.title : '提示',
			icon: h(ExclamationCircleOutlined),
			content: h('div', {
				style: 'font-size:14px;'
			}, option.content),
			okText: option.yText ? option.yText : '确 定',
			cancelText: option.nText ? option.nText : '取 消',
			onOk() {
				if (option.ok) {
					option.ok();
				}
			}
		});
	},
	//上报异常信息
	captureError: (e) => {
		airSentry.captureError(e);
	},
	/**
	 * 保存缓存数据
	 * @param {*} key
	 * @param {*} value
	 */
	saveLocalStorage: (key, value) => {
		localStorage.setItem(key, value);
	},
	/**
	 * 读取缓存数据
	 * @param {*} key
	 * @returns
	 */
	readLocalStorage: (key) => {
		return localStorage.getItem(key) || '';
	},
	/**
	 * 清楚所有缓存
	 */
	clearLocalStorage: () => {
		localStorage.clear();
	},
	/**
	 * 删除一个缓存数据
	 * @param {*} key
	 */
	removeLocalStorage: (key) => {
		localStorage.removeItem(key);
	},
	/**
	 * 加密数据
	 * @param {*} data
	 * @returns
	 */
	encryptData: (data) => {
		return encryptData(data);
	},
	/**
	 * 获取hashCode
	 */
	hashcode: (str) => {
		let hash = 1,
			i,
			chr;
		if (str.length === 0) return hash;
		for (i = 0; i < str.length; i++) {
			chr = str.charCodeAt(i);
			hash = (hash << 5) - hash + chr;
			hash |= 0; // Convert to 32bit integer
		}
		return hash;
	},
	/**
	 * @param {date} time 需要转换的时间
	 * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
	 */
	formatTime: (time, fmt) => {
		if (!time) return '';
		else {
			const date = new Date(time);
			const o = {
				'M+': date.getMonth() + 1,
				'd+': date.getDate(),
				'H+': date.getHours(),
				'm+': date.getMinutes(),
				's+': date.getSeconds(),
				'q+': Math.floor((date.getMonth() + 3) / 3),
				S: date.getMilliseconds(),
			};
			if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
			for (const k in o) {
				if (new RegExp('(' + k + ')').test(fmt)) {
					fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
				}
			}
			return fmt;
		}
	},
	getTimeArray: (type) => {
		if (type == 'a') {
			return ['', ''];
		}
		let time = typeToTime(type);
		if (time) {
			time = time.split('至')
		} else {
			time = ['', ''];
		}
		return time;
	},
	getTime: (type) => {
		if (type == 'a') {
			return '';
		}
		return typeToTime(type);
	},
	getMonth: (date) => {
		if (!date) {
			date = new Date();
		}
		return moment(date).format('YYYY-MM-DD');
	},
	getURLParams:(url)=> {
		if (url.indexOf('#') > 0) {
			url = url.split('#')[0];
		}
		let path = url.split('?')
		if (path.length != 2) {
			return {};
		}
		var paramContent = path[1]; //获取?之后的参数字符串
		var paramsArray = paramContent.split('&'); //参数字符串分割为数组
		var paramResult = {};
		//遍历数组，拿到json对象
		paramsArray.forEach((item, index, paramsArray) => {
			paramResult[paramsArray[index].split('=')[0]] = paramsArray[index].split('=')[1];
		})
		return paramResult;
	}
};
